<!-- 事件日志 -->
<template>
  <div class="dashboard-editor-container">
    <div style="display:flex;justify-content:space-between;">
      <partitionTree :nodeData="nodeData" :expandArray="expandArray" :addTemplate="false" :defaultExpandAll="false"
        dialogName="FileVirusCheck" @handleNodeClick="handleNodeClick" ref="partitionTree"
        @toggleVisibility="toggleVisibility">
      </partitionTree>
      <div :style="{ width: containerWidth }" class="box"
        style="background:#fff;border: 1px solid #ddd;border-radius: 4px;box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);">
        <el-row style="background: #fff; padding: 16px 16px 0;">
          <connsearch ref="connsearch" :formOptions="formOptions" @search="search" @reset="reset">
          </connsearch>
          <div class="tabclick">
            <div class="tabitem" @click="handleClick()">
              <span><i :class="tab ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span>
            </div>
          </div>
          <el-row>
            <el-tabs type="border-card" v-if="tab" style="margin-top:16px;" @tab-click="tabClick">
              <el-tab-pane label="按病毒类型统计分析" lazy>
                <el-col :xs="24" :sm="24" :lg="24">
                  <piechart :chartData="pie3Data" @pieclick="echartsClick" title="按病毒类型统计分析" />
                </el-col>
              </el-tab-pane>
            </el-tabs>
          </el-row>
          <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 24 }" :xl="{ span: 24 }"
            style="padding-right: 8px;margin:16px 0;">
            <el-table stripe border :data="tableData" @selection-change="handleSelectionChange"
              style="width: 100%; margin-top: 15px">
              <el-table-column type="selection" :selectable="isDsiabled" width="55">
              </el-table-column>
              <el-table-column label="序号" width="60" type="index"
                :index='(index) => { return (index + 1) + (this.pageNum - 1) * this.pageSize }'> </el-table-column>
              <el-table-column label="部门" show-overflow-tooltip prop="Department" width="120px"></el-table-column>
              <el-table-column label="资产名称" show-overflow-tooltip prop="DeviceName" width="160px"></el-table-column>
              <el-table-column label="使用者" show-overflow-tooltip prop="UserName" width="120px"></el-table-column>
              <el-table-column label="文件名称" show-overflow-tooltip prop="FileName"></el-table-column>
              <el-table-column label="检测时间" show-overflow-tooltip prop="TransTime" width="180px">
                <template slot-scope="{row}">
                  <span>{{ parseTime(row.TransTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="检测结论" show-overflow-tooltip prop="CheckResult" width="120px">
                <template slot-scope="{row}">
                  <span
                    :style="{ color: row.CheckResult === 1 ? '#67C23A' : row.CheckResult === 2 ? '#F56C6C' : '#909399' }">{{
                      row.CheckResult === 1 ? '正常' : row.CheckResult === 2 ? '病毒' : '等待查杀' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="文件指纹" show-overflow-tooltip prop="FileMD5"></el-table-column>
              <el-table-column label="病毒类型" show-overflow-tooltip prop="VirsutClass"></el-table-column>
              <el-table-column label="进程说明" show-overflow-tooltip prop="FilePath"></el-table-column>
              <el-table-column label="进程描述" show-overflow-tooltip prop="Descp"></el-table-column>
              <el-table-column label="操作" width="160px" fixed="right">
                <template slot="header" slot-scope="scope">
                  <el-button size="mini" type="primary" @click="changeMailGlobalAll(scope.row)">批量修正</el-button>
                </template>
                <template slot-scope="scope">
                  <el-button size="mini" :type="scope.row.CheckResult == '1' ? 'info' : 'primary'"
                    :disabled="scope.row.CheckResult == '1'" @click="changeMailGlobal(scope.row)">误判修正</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page=pageNum
              :page-size=pageSize background layout="total, prev, pager, sizes, next, jumper" :total="total">
            </el-pagination>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import connsearch from "@/components/connSearch";
import barchart from "./components/barchart.vue";
import piechart from "./components/piechart.vue";
import { GetDeviceTree2, parseTime } from "@/utils/index.js";
import { connrequest } from "@/api/firstPage.js"
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import partitionTree from "@/components/partitionTree";
export default {
  directives: { elDragDialog },
  components: { connsearch, barchart, piechart, partitionTree },
  data() {
    let that = this
    return {
      activeName: "",
      tabTitle: '按病毒类型统计分析',
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tab: false,
      FileMD5List: [],
      pie3Data: [],
      pie4Data: [],
      pie5Data: [],
      nodeData: [],
      expandArray: [],
      containerWidth: 'calc(100% - 275px)',
      tableData: [],
      searchform: {},
      options: {},
      nodeobj: {},
      searchobj: {},
      echartsobj: {},
      Moder: {},
      moderVisible: false,
      rules: {},
      partitionList: [],
      dictionaryList: [],
      formOptions: [
        {
          col: 8,
          label: "文件名称",
          prop: "FileName",
          element: "el-input",
          placeholder: "请输入文件名称",
          maxlength: 50,
          events: {
            input(val) {
              that.searchform.FileName = val
            },
          },
        },
        {
          col: 8,
          hidden: false,
          label: "检测结论", // label文字
          prop: "CheckResult", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: 0, // 字段初始值,
          placeholder: "请选择检测结论", // elementui组件属性
          options: [
            { label: '全部', value: 0 },
            { label: '正常', value: 1 },
            { label: '病毒', value: 2 },
            { label: '等待查杀', value: 3 },
          ],
          rules: [], // elementui组件属性
          events: {
            input(val) {
              that.searchform.CheckResult = val
            },
          },
        },
      ],
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.pageSize = (Cookies.get('Zhuoguan_DefaultPageLength') || 10) * 1;

    this.getData()
    this.initData()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
  },
  methods: {
    parseTime,
    async initData(searchobj = {}) {
      const { DeviceTree } = await GetDeviceTree2('FileVirusCheck', searchobj)
      this.nodeData = DeviceTree
      this.expandArray = [DeviceTree?.[0]?.Name]
    },
    handleNodeClick(nodeData) {
      this.pageNum = 1
      this.nodeobj = { DepartmentID: nodeData.data.isDevice ? nodeData.data.DepartmentID : nodeData.data.ID, DeviceUUID: nodeData.data.isDevice ? nodeData.data.DeviceUUID : '' }
      this.echartsobj = {}
      this.getData({ ...this.searchobj, ...this.nodeobj })
    },
    toggleVisibility(isHidden) {
      if (isHidden)
        this.containerWidth = '100%';
      else
        this.containerWidth = 'calc(100% - 275px)';
    },
    isDsiabled(row) {
      return row.CheckResult === 2
    },
    handleSelectionChange(arr) {
      this.FileMD5List = arr.map(item => item.FileMD5);
    },
    async changeMailGlobal(row) {
      const data = {
        Data: [row.FileMD5]
      }
      const res = await connrequest('ModifyRainBowLib', data)
      if (res.Status.StatusCode === 0) {
        await this.getData({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
        this.$message.success('修正成功')
      }
    },
    async changeMailGlobalAll() {
      if (this.FileMD5List.length === 0) {
        this.$message.info('请先选择需要修正的数据')
        return
      }
      const data = {
        Data: this.FileMD5List
      }
      const res = await connrequest('ModifyRainBowLib', data)
      if (res.Status.StatusCode === 0) {
        await this.getData({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
        this.$message.success('修正成功')
      }
    },
    // 获取所有数据
    getData(searchobj = {}) {
      this.GetEventLog(searchobj)
      switch (this.tabTitle) {
        case '按病毒类型统计分析':
          this.GetRainBowLibSecuritySumByClass(searchobj)
          break;
      }
    },
    echartsClick(val, title) {
      this.pageNum = 1
      if (title === '按病毒类型统计分析') {
        this.echartsobj = { VirusType: val }
        this.GetEventLog({ ...this.searchobj, ...this.nodeobj, ...this.echartsobj })
      }
    },
    tabClick(row) {
      this.tabTitle = row.label
      switch (row.label) {
        case '按病毒类型统计分析':
          this.GetRainBowLibSecuritySumByClass({ ...this.searchobj, ...this.nodeobj })
          break;
      }
    },
    // 获取事件日志列表
    async GetEventLog(searchobj = {}) {
      const data = {
        Paging: {
          QueryPageNo: this.pageNum,
          QueryPageLength: this.pageSize
        },
        Filter: {
          FileName: this.searchform.FileName,
          CheckResult: this.searchform.CheckResult,
          ...searchobj
        }
      }
      const res = await connrequest('GetRainBowLibSecurity', data)
      res.Data = res.Data || []
      this.tableData = res.Data
      this.total = res.Paging?.TotalCount || 0
    },
    // 按事件资产统计分析
    async GetRainBowLibSecuritySumByClass(searchobj = {}) {
      let arr = []
      const data = {
        Filter: {
          FileName: this.searchform.FileName,
          CheckResult: this.searchform.CheckResult,
          ...searchobj
        }
      }
      const res = await connrequest('GetRainBowLibSecuritySumByClass', data)
      res.Data = res.Data || []
      res.Data.map(item => {
        arr.push({ name: item.VirsutClassCN, value: item.Count })
      })
      this.pie3Data = arr
    },
    // 搜索
    search(val) {
      this.pageNum = 1
      this.searchobj = { ...val }
      this.searchform = { ...val }
      this.getData({ ...this.echartsobj, ...this.searchobj, ...this.nodeobj })
      this.initData({ ...this.searchobj })
    },
    // 重置
    reset() {
      this.pageNum = 1
      this.searchform = {}
      this.echartsobj = {}
      this.searchobj = {}
      this.nodeobj = {}
      this.$set(this.searchform, 'FileName', 0)
      this.$set(this.searchform, 'CheckResult', '')
      this.$refs.connsearch.addInitValue()
      this.getData()
      this.initData()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.pageNum = 1
      this.GetEventLog({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.GetEventLog({ ...this.searchobj, ...this.echartsobj, ...this.nodeobj })
    },
    handleClick() {
      this.tab = !this.tab
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./components/css/conncss.scss";
</style>